package cn.tedu.account.service;

import cn.tedu.account.entity.Account;
import cn.tedu.account.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Transactional
    @Override
    public void decrease(Long userId, BigDecimal money) {
        Account a = accountMapper.selectByUserId(userId);
        /**
         * BigDecimal  a   BigDecimal  b
         * a.compareTo(b)来比较大小
         *      返回值:
         *          正数,a>b
         *          负数,a<b
         *          0,a=b
         */
        if (a.getResidue().compareTo(money)<0){
            throw new RuntimeException("可用金额不足");
        }
        accountMapper.decrease(userId, money);
    }
}
